package com.servlet;

import com.bean.BookBean;
import com.dao.BooksDao;
import com.dao.SongerService;
import com.dao.impl.BooksDaoImpl;
import com.dao.impl.SongerServiceImpl;
import com.entity.Songer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.servlet.teacher.BaseServlet;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@WebServlet("/SongerServlet")
public class SongerServlet extends BaseServlet {
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SongerService Songer = (SongerService) ProxyUtil.getDaoProxy(new SongerServiceImpl());
        //  HttpSession session =  req.getSession();
        //  Object loginUserName =     session.getAttribute("loginUserInfo");

        //  System.out.println("从session中取出的值为:"+loginUserName);


        //queryDataBypage方法的参数currentPage肯定通过前端传递过来
        //前端如何传递参数给Servlet呢  原来我们用的是提交表单实现的表单 数据是会附加在请求的url后面  ?key=value&key=value
        //   我们只要要url后面添加?key=value就可以传值。。
        String curentPage = req.getParameter("currentPage");
        String nickname = req.getParameter("nickname");

        Songer songer = new Songer();
        songer.setNickname(nickname);//把拿到的请求参数放到JavaBean中


        JsonUtil jsonUtil = new JsonUtil();
        try {
            PageUtil<com.entity.Songer> songerPageUtil = Songer.queryDataBypage(Integer.parseInt(curentPage), 2, songer);
            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(songerPageUtil);


        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:" + e.getMessage());

        }



        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();

        String jsonStr = gson.toJson(jsonUtil);
        System.out.println("转换后的json串为:" + jsonStr);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();


        out.print(jsonStr);//把json字符串输出给客户端

    }

    //单行删除
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String songerId =   req.getParameter("songerId");
        System.out.println("我要删除的id"+songerId);
        //调用Dao的删除方法
        SongerService Songer = (SongerService) ProxyUtil.getDaoProxy(new SongerServiceImpl());

        JsonUtil jsonUtil = new JsonUtil();
        try {
            int res =  Songer.deleteById(songerId);

            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");
        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }
    //批量删除
    public void deleteByIdS(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SongerService Songer = (SongerService) ProxyUtil.getDaoProxy(new SongerServiceImpl());
        resp.setContentType("text/html;charset=utf-8");
        String ids = req.getParameter("id");
        System.out.println(ids);

        Gson gson = new Gson();
        JsonUtil jsonUtil = new JsonUtil();


        try {
            if(ids.endsWith("]")) {
                ids = ids.substring(1, ids.length()-1);

            }

            String[] split = ids.split(",");



            int i = Songer.deletedIds(split);
            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");
        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("失败");
        }
        String toJson = gson.toJson(jsonUtil);

        PrintWriter writer = resp.getWriter();
        writer.print(toJson);
    }

    public void addSinger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SongerService Songer = (SongerService) ProxyUtil.getDaoProxy(new SongerServiceImpl());
        System.out.println("我准备添加了奥");
        JsonUtil jsonUtil = new JsonUtil();
        try {
            ConvertUtils.register(new DateLocaleConverter(),Date.class);
            Songer songer = new Songer();


            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的Songer对象中的属性值为:"+songer);
            BeanUtils.populate(songer,req.getParameterMap());
            System.out.println("反射后的Songer对象中的属性值为:"+songer);

            int res =  Songer.insert(songer);

            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");






        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SongerService Songer = (SongerService) ProxyUtil.getDaoProxy(new SongerServiceImpl());

        JsonUtil jsonUtil = new JsonUtil();



        try {
            DateConverter converter = new DateConverter();
            converter.setPattern(new String("yyyy-MM-dd"));
            ConvertUtils.register(converter, Date.class);
            Songer songer = new Songer();


            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的Songer对象中的属性值为:"+songer);
            BeanUtils.populate(songer,req.getParameterMap());
            System.out.println("反射后的Songer对象中的属性值为:"+songer);

            int res =  Songer.update(songer);

            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");






        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }





}



